<template>
	<view>
		<view class="search-box">
			<uni-search-bar v-model="kw" @confirm="getSearchList" :radius="100" cancelButton="none"
				placeholder="查找搜索XXX号/手机号">
			</uni-search-bar>
		</view>

		<!-- 搜索历史 -->
		<view class="history-box">
			<!-- 标题区域 -->
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="clean"></uni-icons>
			</view>
			<!-- 列表区域 -->
			<view class="history-list">
				<uni-tag :text="item" v-for="(item,index) in histories" :key="index" @click="changeSearchInput(item)">

				</uni-tag>
			</view>
		</view>

		<!-- 搜索建议列表 -->
		<view class="sugg-list" v-if="searchResults.length !== 0">
			<search-item :searchResults="searchResults"></search-item>
		</view>
	</view>
</template>

<script>
	import uniSearchBar from '@/uni_modules/uni-search-bar/components/uni-search-bar/uni-search-bar.vue'
	import uniIcons from '@/uni_modules/uni-icons/components/uni-icons/uni-icons.vue'
	import uniTag from '@/uni_modules/uni-tag/components/uni-tag/uni-tag.vue'

	import searchItem from '@/pages/msg/search/search-child/search-item.vue'

	import {
		searchByCondition
	} from '@/api/http/main/my/user.js'

	export default {
		data() {
			return {
				timer: null,
				kw: null,
				searchResults: [],
				//搜索关键词的历史记录
				historyList: ['a', 'app', 'apple'],
				userId: uni.getStorageSync("userId")
			};
		},
		components: {
			uniSearchBar,
			uniIcons,
			uniTag,
			searchItem
		},
		methods: {
			getSearchList(e) {
				//console.log('开始搜索')
				// console.log(e)
				this.kw = e.value
				//判断搜索关键词是否为空
				if (this.kw.length === 0) {
					this.searchResults = []
					return
				}
				if (this.kw == uni.getStorageSync("username") || this.kw == uni.getStorageSync("phone")) {
					return uni.$showMsg('无法搜索自己的账号！')
				}
				var that = this
				searchByCondition(parseInt(this.userId), this.kw).then(res => {
					if (res.data.pubInfo.code !== 0) return uni.$showMsg(res.data.pubInfo.message)

					//数据处理
					var searchData = res.data.busInfo
					//console.log(searchData)
					if (searchData.length == 0) {
						return
					}

					for (var i = 0; i < searchData.length; i++) {
						if (i == 0) {
							continue;
						}
						if (searchData[i].title == searchData[i - 1].title) {
							searchData[i].title = ''
						}
					}

					that.searchResults = searchData
				})

				this.saveSearchHistory()
			},

			gotoDetail(user) {
				//查看用户详情
				//console.log(123)
				uni.navigateTo({
					url: '/pages/user/detail/detail?userId=' + user.userId
				})
			},
			saveSearchHistory() {
				//this.historyList.push(this.kw)

				//1，将array数组转化为set对象
				const set = new Set(this.historyList)
				//2，调用set对象的delete方法，移除对应的元素
				set.delete(this.kw)
				//3，调用set对象的add方法，向set中添加元素
				set.add(this.kw)
				//4，将set对象转化为array数组
				this.historyList = Array.from(set)

				//对搜索历史数据进行持久化存储
				uni.setStorageSync('kw', JSON.stringify(this.historyList))
			},
			clean() {
				this.historyList = []
				uni.setStorageSync('kw', '[]')
			},
			changeSearchInput(item) {
				//console.log(item)
				this.kw = item
			}
		},
		computed: {
			//通过计算属性，生成新的数组，并将数组顺序翻转
			histories() {
				return [...this.historyList].reverse()
			}
		},
		onLoad() {
			//this.historyList = JSON.parse(pgetStorageSync('kw') || '[]')
			var historyList = uni.getStorageSync('kw')
			//console.log(historyList || [])
			this.historyList = JSON.parse(historyList || '[]')
			
		}
	}
</script>

<style lang="scss" scoped>
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
	}

	.sugg-list {
		padding: 0 5px;

		.sugg-item {
			font-size: 32rpx;
			padding: 30rpx 0;
			border-bottom: 1px solid #efefef;
			display: flex;
			align-items: center;

			// justify-content: space-between;
			.sugg-item-left {
				width: 20%;

				image {
					width: 88rpx;
					height: 88rpx;
				}

				margin-left: 10rpx;
			}

			.sugg-item-center {
				width: 60%;
				//文字不允许换行（单行文本）
				white-space: nowrap;
				//溢出部分隐藏
				overflow: hidden;
				//文本溢出后，使用。。。代替
				text-overflow: ellipsis;
				margin-right: 3px;
			}

			.sugg-item-right {
				width: 20%;

				view {
					width: 100%;
					height: 64rpx;
					font-size: 24rpx;
					line-height: 64rpx;
					text-align: center;
					border-radius: 40rpx;
				}

				.button-top {
					background-color: #ffd300;
					color: #FFFFFF;
				}

				.button-bottom {
					background-color: #3d66b8;
					color: #FFFFFF;
				}
			}
		}


	}

	.history-box {
		padding: 0 5px;

		.history-title {
			display: flex;
			justify-content: space-between;
			height: 40px;
			align-items: center;
			font-size: 13px;
			border-bottom: 1px solid #efefef;
		}

		.history-list {
			display: flex;
			flex-wrap: wrap;

			.uni-tag {
				margin-top: 5px;
				margin-right: 5px;
				background-color: #FFFFFF;
				border: none;
				color: #000000;
			}
		}
	}
</style>
